﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace sand_game_core
{

    public abstract class particle
    {
        public uint x;
        public uint y;
        public static Texture2D myImage;
        private Color baseColor;
        public Color myColor;
        public bool active;
        public particle toTransition;
        public int light;
        protected static particle upParticle;
        protected static particle downParticle;
        protected static uint ammount;
        protected static particle RightParticle;
        protected static particle LeftParticle;

        public delegate void Update(ref particle p);
        public static Update toUpdate;
        public delegate void BaseLight(ref particle p);
        public static BaseLight Light;

        public particle(Color c, int startlight)
        {
            baseColor = c;
            myColor = c;
            light = startlight;
            active = true;
            toTransition = null;
            toUpdate = new Update(updating);
            Light = new BaseLight(baseLight);
        }
        private void finalize()
        {
            myColor.PackedValue = (uint)(
                (myColor.A << 24) +
                ((baseColor.B - (255 - light) > 0 ? (baseColor.B -(255 - light) ) : 0) << 16) +
                ((baseColor.G - (255 - light) > 0 ? (baseColor.G - (255 - light)) : 0) << 8) +
                 (baseColor.R - (255 - light) > 0 ? (baseColor.R - (255 - light)) : 0));
        }
        public static void updating(ref particle p)
        {
            p.update();
        }
        public abstract void update();
        private static void baseLight(ref particle ThisParticle)
        {
            if (ThisParticle != null)
            {
                upParticle = world.arrayheight > ThisParticle.y + 1 ? world.worldParticleLayer[ThisParticle.x * world.arraywidth + ThisParticle.y + 1] : null;
                downParticle = ThisParticle.y >= 1 ? world.worldParticleLayer[ThisParticle.x * world.arraywidth + ThisParticle.y - 1] : null;
                RightParticle = ThisParticle.x >= 1 ? world.worldParticleLayer[(ThisParticle.x - 1) * world.arraywidth + ThisParticle.y] : null;
                LeftParticle = world.arraywidth > ThisParticle.y + 1 ? world.worldParticleLayer[(ThisParticle.x + 1) * world.arraywidth + ThisParticle.y] : null;
                ThisParticle.light = downParticle != null ? (downParticle.light > ThisParticle.light ? downParticle.light : ThisParticle.light) : ThisParticle.light;
                ThisParticle.light = LeftParticle != null ? (LeftParticle.light > ThisParticle.light ? LeftParticle.light : ThisParticle.light) : ThisParticle.light;
                ThisParticle.light = RightParticle != null ? (RightParticle.light > ThisParticle.light ? RightParticle.light : ThisParticle.light) : ThisParticle.light;
                ThisParticle.light = upParticle != null ? (upParticle.light > ThisParticle.light ? upParticle.light : ThisParticle.light) : ThisParticle.light;/*
            else
            {
                light = 255;
            }*/
                ThisParticle.lightMe();
                ThisParticle.finalize();
            }

        }
        private static byte myClamp(int x)
        {
            return (byte)(x < 0 ? 0 : (x > 255 ? 255 : x));
            /*
            if (input >= 0 && input < 255)
                return (byte)input;
            if (input > 255)
                return 255;
            return 0;*/
        }
        public abstract void lightMe();
        public abstract void reactivate();
    }
}
